package com.oa.modules.approvalsec1.controller;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.modules.approvalsec1.entity.CategoryEntity;
import com.oa.modules.approvalsec1.entity.ProjectEntity;
import com.oa.modules.approvalsec1.service.CategoryService;
import com.oa.modules.approvalsec1.service.ProjectService;
import com.oa.modules.sys.controller.AbstractController;
import com.oa.modules.sys.entity.SysRoleEntity;
import com.oa.modules.sys.entity.SysUserRoleEntity;
import com.oa.modules.sys.service.SysRoleService;
import com.oa.modules.sys.service.SysUserRoleService;
import com.oa.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;

/**
 *
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-12-27 10:37:11
 */
@RestController
@RequestMapping("category")
@Api(tags =  "项目分类相关接口")
public class CategoryController extends AbstractController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);
        List<CategoryEntity> list = (List<CategoryEntity>) page.getList();
        SysUserRoleEntity userRoleEntity = sysUserRoleService.getOne(new QueryWrapper<SysUserRoleEntity>().eq("user_id", params.get("userId")));
        list.forEach(item->{
            QueryWrapper<ProjectEntity> wrapper = new QueryWrapper<ProjectEntity>().eq("type", item.getId());
            if(userRoleEntity.getRoleId()!=19){
                wrapper.eq("is_important",0);
            }else{
                wrapper.eq("is_important",1);
            }
            long count = projectService.list(wrapper).stream().count();
            item.setCount(count);
        });
        page.setList(list);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") Integer id){
		CategoryEntity category = categoryService.getById(id);
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody CategoryEntity category){
        category.setCreateDate(new Date());
        category.setCreateOper(getUserId().toString());
		categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody Integer[] ids){
		categoryService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
